package com.example.lending.View;


import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lending.Model.User;
import com.example.lending.R;
import com.example.lending.ViewModel.UserViewModel;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * A simple {@link Fragment} subclass.
 */
public class WalletFragment extends Fragment {
    EditText etYue;
    TextView tvYue;
    Button btYue;
    UserViewModel userViewModel;
    String money;
    User user;

    public WalletFragment() {
        // Required empty public constructor
    }

    public void inteYue(){
        etYue=getView().findViewById(R.id.et_input_yue);
        tvYue=getView().findViewById(R.id.tv_yue);
        btYue=getView().findViewById(R.id.btn_ok);
        user=new User();
        this.userViewModel= ViewModelProviders.of(getActivity()).get(UserViewModel.class);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        inteYue();

        etYue.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                tvYue.setText(s.toString());
                money=s.toString();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        btYue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                final String username=getActivity().getSharedPreferences("user", Context.MODE_PRIVATE).getString("username","");
                System.out.println("xxxxxxxxx"+" "+username);
                if(TextUtils.isEmpty(money)){
                    Toast.makeText(getActivity(), "请填写您要充值的金额", Toast.LENGTH_SHORT).show();
                }else {
                    final LiveData<List<User>>liveusers= userViewModel.findUserWithPattern(username);
                    final int mo=Integer.parseInt(money);
                    System.out.println("aaaaaaaaaaa"+liveusers.hashCode());
                    liveusers.removeObservers(getViewLifecycleOwner());
                    liveusers.observe(getViewLifecycleOwner(), new Observer<List<User>>() {
                        @Override
                        public void onChanged(List<User> users) {
                            System.out.println("nnnnnnnnnnnnnnnn");
                            //for(int i=0;i<users.size();i++){
                            int i=0;
                                user.setAge(users.get(i).getAge());
                                user.setClassnum(users.get(i).getClassnum());
                                user.setType(users.get(i).getType());
                                user.setPassword(users.get(i).getPassword());
                                user.setName(users.get(i).getName());
                                user.setEmail(users.get(i).getEmail());
                                user.setSex(users.get(i).getSex());
                                user.setMoney(users.get(i).getMoney()+mo);
                                users.get(i).setMoney(user.getMoney());
                                userViewModel.updataUsers(users.get(i));
                                //userViewModel.insertUsers(user);
                                //userViewModel.deleteUsers(users.get(i));
                                Toast.makeText(getActivity(), "充值完成", Toast.LENGTH_SHORT).show();
                            //}
                            System.out.println("mmmmmmmmmmmmmmmmmm");
                            NavController controller=Navigation.findNavController(v);
                            controller.navigate(R.id.action_walletFragment_to_homeFragment);
                        }
                    });
                }
            }
        });

    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_wallet, container, false);
    }

}
